package com.ticket.web.controller.yanchu;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ticket.common.core.domain.model.LoginUser;
import com.ticket.common.utils.StringUtils;
import com.ticket.framework.web.service.TokenService;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ticket.common.annotation.Log;
import com.ticket.common.core.controller.BaseController;
import com.ticket.common.core.domain.AjaxResult;
import com.ticket.common.enums.BusinessType;
import com.ticket.system.domain.SysNoticeUser;
import com.ticket.system.service.ISysNoticeUserService;
import com.ticket.common.utils.poi.ExcelUtil;
import com.ticket.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * @date 2023-04-10
 */
@RestController
@RequestMapping("/system/noticeUser")
public class SysNoticeUserController extends BaseController
{
    @Autowired
    private ISysNoticeUserService sysNoticeUserService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysNoticeUser sysNoticeUser)
    {
        startPage();
        List<SysNoticeUser> list = sysNoticeUserService.selectSysNoticeUserList(sysNoticeUser);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:user:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysNoticeUser sysNoticeUser)
    {
        List<SysNoticeUser> list = sysNoticeUserService.selectSysNoticeUserList(sysNoticeUser);
        ExcelUtil<SysNoticeUser> util = new ExcelUtil<SysNoticeUser>(SysNoticeUser.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysNoticeUserService.selectSysNoticeUserById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:user:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysNoticeUser sysNoticeUser)
    {
        return toAjax(sysNoticeUserService.insertSysNoticeUser(sysNoticeUser));
    }

    /**
     * 未读消息数
     */
    @Log(title = "未读消息数", businessType = BusinessType.OTHER)
    @GetMapping("/unreadCount")
    @ApiOperation("未读消息数")
    public Map unreadCount(HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysNoticeUser sysNoticeUser = new SysNoticeUser();
        sysNoticeUser.setUserId(loginUser.getUser().getUserId());
        int count = sysNoticeUserService.unreadCount(sysNoticeUser);
        Map map = new HashMap();
        map.put("count",count);
        return map;
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysNoticeUser sysNoticeUser)
    {
        return toAjax(sysNoticeUserService.updateSysNoticeUser(sysNoticeUser));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:user:remove')")
    @Log(title = "【删除】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String ids)
    {
        return toAjax(sysNoticeUserService.deleteSysNoticeUserByIds(ids));
    }

    /**
     * 已读
     */
    @ApiOperation("已读")
    @Log(title = "已读", businessType = BusinessType.UPDATE)
    @GetMapping("/read/{ids}")
    public AjaxResult read(@PathVariable String ids)
    {
        if(StringUtils.isBlank(ids)){
            return error("参数为空");
        }
        return toAjax(sysNoticeUserService.readByIds(ids));
    }

    /**
     * 所有已读
     */
    @Log(title = "已读", businessType = BusinessType.UPDATE)
    @GetMapping("/readAll")
    @ApiOperation("所有已读")
    public AjaxResult readAll(HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        return toAjax(sysNoticeUserService.readALl(loginUser.getUser().getUserId()));
    }


    /**
     * 获取【强提醒第一条】详细信息
     */
    @ApiOperation("获取【强提醒第一条】详细信息")
    @GetMapping(value = "/getForceNoticeByUser")
    public AjaxResult getForceNoticeByUser(HttpServletRequest request, Integer  pushChannel)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        return success(sysNoticeUserService.getForceNoticeByUser(loginUser.getUser().getUserId(),pushChannel));
    }

    /**
     * 小程序获取用户消息详细信息
     */
    @ApiOperation("小程序获取用户消息详细信息")
    @GetMapping(value = "/getNoticeInfoByUser")
    public AjaxResult getNoticeInfoByUser(HttpServletRequest request, Long  noticeId)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        return success(sysNoticeUserService.getNoticeInfoByUser(loginUser.getUser().getUserId(), noticeId));
    }
}
